home *** CD-ROM | disk | FTP | other *** search
/ The Java 3D API Specification (2nd Edition) / The Java 3D API Specification (2nd Edition).iso / programs / examples / Sound / SimpleSoundsBehavior.java < prev    next >
Text File  |  2000-04-28  |  7KB  |  164 lines

  1. /*
  2.  *    @(#)SimpleSoundsBehavior.java 1.12 00/02/10 13:15:05
  3.  *
  4.  * Copyright (c) 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import javax.media.j3d.*;
  32. import javax.vecmath.*;
  33. import java.net.URL;
  34. import java.util.Enumeration;
  35.  
  36. // User defined audio behavior class
  37. public class SimpleSoundsBehavior extends Behavior {
  38.     WakeupOnElapsedTime  wt;
  39.     WakeupOnBehaviorPost wp;
  40.         BackgroundSound      sound1 = new BackgroundSound();
  41.         PointSound           sound2 = new PointSound();
  42.         PointSound           sound3 = new PointSound();
  43.         static int           WAKEUP_SOUND = 0;
  44.         int                  soundIndex = 0;
  45.         URL                  URLName1;
  46.         URL                  URLName2;
  47.         URL                  URLName3;
  48.         BoundingSphere       bounds;
  49.  
  50.     // Override Behavior's initialize method to setup wakeup criteria
  51.     public void initialize() {
  52.             MediaContainer sample1  = new MediaContainer();
  53.             MediaContainer sample2  = new MediaContainer();
  54.             MediaContainer sample3  = new MediaContainer();
  55.             sample1.setCapability(MediaContainer.ALLOW_URL_WRITE);
  56.             sample1.setCapability(MediaContainer.ALLOW_URL_READ);
  57.             sample1.setURLObject(URLName1);
  58.             sample1.setCacheEnable(false);
  59.             sound1.setLoop(0);
  60.             sound1.setContinuousEnable(false);
  61.             sound1.setReleaseEnable(false);
  62.             sound1.setSoundData(sample1);
  63.             sound1.setInitialGain(0.7f);
  64.             sample2.setCapability(MediaContainer.ALLOW_URL_WRITE);
  65.             sample2.setCapability(MediaContainer.ALLOW_URL_READ);
  66.             sample2.setURLObject(URLName2);
  67.             sound2.setLoop(Sound.INFINITE_LOOPS);
  68.             sound2.setContinuousEnable(false);
  69.             sound2.setReleaseEnable(false);
  70.             sound2.setSoundData(sample2);
  71.             sound2.setInitialGain(2.0f);
  72.             Point3f sound2Pos = new Point3f(-30.0f, 0.0f, 0.0f);
  73.             sound2.setPosition(sound2Pos);
  74.             sample3.setCapability(MediaContainer.ALLOW_URL_WRITE);
  75.             sample3.setCapability(MediaContainer.ALLOW_URL_READ);
  76.             sample3.setURLObject(URLName3);
  77.             sound3.setContinuousEnable(false);
  78.             sound3.setReleaseEnable(false);
  79.             sound3.setSoundData(sample3);
  80.             sound3.setInitialGain(4.0f);
  81.             Point3f sound3Pos = new Point3f(30.0f, 0.0f, 0.0f);
  82.             sound3.setPosition(sound3Pos);
  83.  
  84.         wt = new WakeupOnElapsedTime(2000);
  85.         WakeupOnElapsedTime wp = new WakeupOnElapsedTime(5000);
  86.         wakeupOn(wp); 
  87.     }
  88.  
  89.     // Override Behavior's stimulus method to handle the event
  90.     public void processStimulus(Enumeration criteria) {
  91.  
  92.             switch (soundIndex)
  93.             {
  94.                 // Active
  95.                 case 0:
  96.                     // System.out.println("****Enable First Sound");
  97.                     sound1.setEnable(true);
  98.                 wakeupOn(wt);
  99.                     break;
  100.                 case 1:
  101.                     // System.out.println("********Enable Second Sound");
  102.                     sound2.setEnable(true);
  103.                 wakeupOn(wt);
  104.                     break;
  105.                 case 2:
  106.                 case 4:
  107.                 case 6:
  108.                 case 8:
  109.                 case 10:
  110.                     // System.out.println("************Enable Third Sound");
  111.                     sound3.setEnable(true);
  112.                 wakeupOn(wt);
  113.                     break;
  114.                 case 3:
  115.                 case 5:
  116.                 case 7:
  117.                 case 9:
  118.                     // System.out.println("************Disable Third Sound");
  119.                     sound3.setEnable(false);
  120.                 wakeupOn(wt);
  121.                     break;
  122.  
  123.                 case 11:
  124.                     // System.out.println("********Disable Second Sound");
  125.                     sound2.setEnable(false) ;
  126.                 wakeupOn(wt);
  127.                     break;
  128.                 case 12:
  129.                     // System.out.println("****Disable First Sound");
  130.                     sound1.setEnable(false) ;
  131.                     // System.out.println("****Test Complete****");
  132.                     wt = new WakeupOnElapsedTime(400000);
  133.                 wakeupOn(wt);
  134.                     break;
  135.  
  136.                 default:
  137.                     break;  
  138.             }
  139.             soundIndex++;
  140.     }
  141.  
  142.         //
  143.     // Constructor for rotation behavior.  
  144.         // Parameters: sound node
  145.         //             sample file name
  146.         //             sound node's bounds
  147.         //
  148.     public SimpleSoundsBehavior(BackgroundSound sound1, 
  149.                                 PointSound sound2, 
  150.                                 PointSound sound3, 
  151.                                     URL urlName1,
  152.                                     URL urlName2,
  153.                                     URL urlName3,
  154.                                     BoundingSphere soundBounds) {
  155.             this.sound1 = sound1;
  156.             this.sound2 = sound2;
  157.             this.sound3 = sound3;
  158.             this.URLName1 = urlName1;
  159.             this.URLName2 = urlName2;
  160.             this.URLName3 = urlName3;
  161.             this.bounds = (BoundingSphere)soundBounds.clone();
  162.     }
  163. }
  164.